package com.dsw.demo.rabbitmq.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * DirectRabbitMQ 配置类
 *
 * @author keyu.wang
 * @date 2018/5/7 10:40
 */
@Configuration
public class DirectRabbitMQConfig {

    public static final String DIRECTEXCHANGE = "direct.exchange";//消息交换机的名字
    public static final String DIRECTQUEUE = "direct.queue";//消息队列名称
    public static final String DIRECTROUTINGKEY = "direct.routing.key";//消息队列routingkey


    /**
     * 配置消息交换机 DirectExchange:按照routingkey分发到指定队列
     *
     * @param
     * @author keyu.wang
     * @date 2018/5/7 16:38
     */
    @Bean(name = "DirectExchange")
    public DirectExchange directExchange() {
        return new DirectExchange(DIRECTEXCHANGE, true, false);
    }


    /**
     * 配置消息队列
     *
     * @author keyu.wang
     * @date 2018/5/7 16:38
     */
    @Bean(name = "DirectQueue")
    public Queue queue() {
        return new Queue(DIRECTQUEUE, true); //队列持久

    }


    /**
     * 将消息队列与交换机绑定
     *
     * @param directExchange 交换机
     * @param directQueue    队列
     * @author keyu.wang
     * @date 2018/5/7 16:37
     */
    @Bean
    public Binding bindingQueue2Exchange(@Qualifier("DirectExchange") DirectExchange directExchange, @Qualifier("DirectQueue") Queue directQueue) {
        return BindingBuilder.bind(directQueue).to(directExchange).with(DirectRabbitMQConfig.DIRECTROUTINGKEY);
    }

}
